package cn.edu.sdjzu.xg.eagle.service;

import cn.edu.sdjzu.xg.eagle.dao.TeacherDaoImpl;
import cn.edu.sdjzu.xg.eagle.dao.TeacherDaoInterface;
import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class TeacherServiceImpl implements TeacherServiceInterface {
    private static final TeacherDaoInterface teacherDao =  TeacherDaoImpl.getInstance();
    private static final TeacherServiceImpl teacherService = new TeacherServiceImpl();

    public static TeacherServiceImpl getInstance() {
        return teacherService;
    }

    @Override
    public boolean add(Teacher teacher) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            added = teacherDao.add(connection, teacher);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            System.out.println("添加过程中发生异常：" + e.getMessage());
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return added;
    }

    @Override
    public boolean delete(Long teacherId) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean deleted = false;
        try {
            deleted = teacherDao.delete(connection, teacherId);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            System.out.println("删除过程中发生异常：" + e.getMessage());
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return deleted;
    }

    @Override
    public Teacher find(Long teacherId) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Teacher teacher = null;
        try {
            teacher = teacherDao.find(teacherId);
        } catch (SQLException e) {
            System.out.println("查找过程中发生异常：" + e.getMessage());
        } finally {
            JdbcHelper.close(null, connection);
        }
        return teacher;
    }

    @Override
    public Collection<Teacher> findAll(List<Condition> conditionList) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = TeacherDaoImpl.getInstance().findAll(connection, conditionList);
        JdbcHelper.close(connection);
        return teachers;
    }

    @Override
    public Collection<Teacher> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = TeacherDaoImpl.getInstance().findAll(connection, conditionList, pagination);
        JdbcHelper.close(connection);
        return teachers;
    }
}
